﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.PrintService;
using CoreShop.Services;
using Newtonsoft.Json;
using SqlSugar.Extensions;
using Sunny.UI;
using System;

namespace CoreShop.App
{
    public partial class frmFoodSoldoutEdit : UIForm
    {
        //private long foodId;
        //private long specId;
        private corecmsfoodspec _spec;
        private corecmsfoodsoldout foodsoldout = null;
        public frmFoodSoldoutEdit(corecmsfoodspec spec)
        {
            //foodId = spec.foodId;
            //specId = spec.id;
            _spec = spec;
            InitializeComponent();
            txtSoldout.TextBox.GotFocus += txtSoldout_GotFocus;
            txtRecoveryQty.TextBox.GotFocus += txtRecoveryQty_GotFocus;
        }


        private void txtSoldout_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 0);
        }

        private void txtRecoveryQty_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 0);
        }

        private void frmSystemSetup_Load(object sender, EventArgs e)
        {
            foodsoldout = new corecmsfoodsoldoutServices().QueryFoodSoldoutById(_spec.id);
            if (foodsoldout != null)
            {
                pnlFoodName.Text = foodsoldout.foodName;
                txtSoldout.Text = CommFunc.FormatDecimal(foodsoldout.soldout);
                txtRecoveryQty.Text = CommFunc.FormatDecimal(foodsoldout.recoveryQty);

                if (foodsoldout.autoSoldout == (int)GlobalEnumVars.AutoSoldout.Repeat)
                    rbtAutoSoldout0.Checked = true;

                if (foodsoldout.soldoutType == (int)GlobalEnumVars.SoldoutType.Market)
                    btnFoodMarket_Click(this.btnFoodMarket, null);
                else if (foodsoldout.soldoutType == (int)GlobalEnumVars.SoldoutType.Class)
                    btnFoodMarket_Click(this.btnClass, null);
                else if (foodsoldout.soldoutType == (int)GlobalEnumVars.SoldoutType.Day)
                    btnFoodMarket_Click(this.btnDate, null);
            }
            else
            {
                var foodinfo = new corecmsfoodServices().GetById(_spec.foodId);

                if (foodinfo != null)
                {
                    pnlFoodName.Text = foodinfo.name;
                    foodsoldout = new corecmsfoodsoldout();
                    foodsoldout.foodId = foodinfo.id;
                    foodsoldout.foodSpecId = _spec.id;
                    foodsoldout.soldoutType = (int)GlobalEnumVars.SoldoutType.Market;
                    foodsoldout.autoSoldout = (int)GlobalEnumVars.AutoSoldout.Repeat;
                    foodsoldout.soldout = 0;
                    foodsoldout.recoveryQty = 0;
                }
                btnFoodMarket_Click(this.btnClass, null);
            }

        }

        private void FOrders_Shown(object sender, System.EventArgs e)
        {
            txtSoldout.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFoodMarket_Click(object sender, EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            btn.Selected = true;
            int tagId = CommFunc.ConvertToInt32(btn.Tag);
            pnlSoldoutType.Text = EnumHelper.GetDescByValue<GlobalEnumVars.SoldoutType>(tagId);
            pnlSoldoutCycle.Text = EnumHelper.GetDescByValue<GlobalEnumVars.SoldoutCycle>(tagId);
            if (foodsoldout != null) foodsoldout.soldoutType = tagId;
            SetSoldoutType();
        }

        private void SetSoldoutType()
        {
            if (rbtAutoSoldout1.Checked)
            {
                pnlSoldoutCycle.Visible = false;
                txtRecoveryQty.Visible = false;
                if (foodsoldout != null) foodsoldout.autoSoldout = (int)GlobalEnumVars.AutoSoldout.One;
            }
            else
            {
                if (foodsoldout != null) foodsoldout.autoSoldout = (int)GlobalEnumVars.AutoSoldout.Repeat;
                pnlSoldoutCycle.Visible = true;
                txtRecoveryQty.Visible = true;
            }
        }

        private void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            try
            {
                double fInput;
                if (string.IsNullOrEmpty(txtSoldout.Text)) txtSoldout.Text = "0";
                if (string.IsNullOrEmpty(txtRecoveryQty.Text)) txtRecoveryQty.Text = "0";
                if (!double.TryParse(txtSoldout.Text, out fInput))
                {
                    UIMessageBox.Show("请输入有效的数字。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                    return;
                }
                if (!double.TryParse(txtRecoveryQty.Text, out fInput))
                {
                    UIMessageBox.Show("请输入有效的数字。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                    return;
                }
                if (foodsoldout == null)
                {
                    UIMessageBox.Show("保存失败，没有生成沽清数据。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                    return;
                }
                foodsoldout.soldout = CommFunc.ConvertToDecimal(txtSoldout.Text);
                foodsoldout.recoveryQty = CommFunc.ConvertToDecimal(txtRecoveryQty.Text);
                if (foodsoldout.autoSoldout == 0)
                {
                    if (foodsoldout.soldout > foodsoldout.recoveryQty)
                    {
                        UIMessageBox.Show("可售数量不能大于限售数量。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                        return;
                    }
                }

                if (foodsoldout.soldoutType == (int)GlobalEnumVars.SoldoutType.Day)
                    foodsoldout.recoveryTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString());

                var bl = new corecmsfoodsoldoutServices().InsertOrUpdate(foodsoldout);
                if (!bl)
                {
                    UIMessageBox.Show("提交保存失败。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                    return;
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message, "错误:", UIStyle.Green, UIMessageBoxButtons.OK);
            }
        }

        private void rbtAutoSoldout1_CheckedChanged(object sender, EventArgs e)
        {
            SetSoldoutType();
        }

        private void btnDeleteSoldout_Click(object sender, EventArgs e)
        {
            if (foodsoldout == null)
            {
                UIMessageBox.Show("删除失败，没有生成沽清数据。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }
            var bl = new corecmsfoodsoldoutServices().DeleteById(foodsoldout.id);
            if (!bl)
            {
                UIMessageBox.Show("提交保存失败。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
